# coding=utf-8
"""
Created on 2018年09月14日
"""
import json

import boto3
from setting_base.setting import C
from common_base.constants import Platform


class AwsSNSService(object):
    """
    亚马逊notification服务
    参考文档: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sns.html
    """

    def __init__(self):
        """
        """
        self.push_config = C('push')
        self.ios_application_arn = self.push_config['snsPlatformArn']['iOS']
        self.android_application_arn = self.push_config['snsPlatformArn']['android']

    def sns_client(self):
        """
        """
        return boto3.client('sns')

    def sns_get_endpoint_attributes(self, endpoint_arn):
        """
        Retrieves the endpoint attributes for a device on one of the
        supported push notification services,such as GCM and APNS.
        """
        attributes = None
        try:
            res = self.sns_client().get_endpoint_attributes(
                EndpointArn=endpoint_arn
            )
            if res and res['ResponseMetadata']['HTTPStatusCode'] == 200:
                attributes = res['Attributes']
        except:
            pass

        return attributes

    def sns_delete_endpoint(self, endpoint_arn):
        """
        Deletes the endpoint for a device and mobile app from Amazon SNS.
        """
        self.sns_client().delete_endpoint(
            EndpointArn=endpoint_arn
        )

    def sns_create_platform_endpoint(self, platform, token, attributes=None):
        """
        Creates an endpoint for a device and mobile app on one of the
        supported push notification services, such as GCM and APNS.

        platform: 1-android, 2-ios
        """
        platformApplicationArn = self.ios_application_arn if platform == Platform.Ios else self.android_application_arn
        customUserData = 'Alive Service test'  # 原作者将其写死了

        endpoint_arn = None
        try:
            res = self.sns_client().create_platform_endpoint(
                PlatformApplicationArn=platformApplicationArn,
                Token=token,
                CustomUserData=customUserData,
                Attributes=attributes if isinstance(attributes, dict) else dict()
            )

            if res and res['ResponseMetadata']['HTTPStatusCode'] == 200:
                endpoint_arn = res['EndpointArn']
        except:
            pass

        return endpoint_arn

    def sns_publish(self, endpoint_arn, message, message_structure='json'):
        """
        Sends a message to an Amazon SNS topic or sends a text
        message (SMS message) directly to a phone number.
        """
        message_id = None
        try:
            res = self.sns_client().publish(
                TargetArn=endpoint_arn,
                Message=message,
                MessageStructure=message_structure,

            )
            if res and res['ResponseMetadata']['HTTPStatusCode'] == 200:
                message_id = res['MessageId']
                print(str(message_id))
        except:
            pass

        return message_id


if __name__ == '__main__':
    aws_sns_service = AwsSNSService()
    endpoint_arn = 'arn:aws:sns:us-west-2:085307523710:endpoint/APNS/AliveService-iOS/086df865-f89f-3342-a069-08b1ab84e870'

    # 获取endpoint信息
    # r = aws_sns_service.sns_get_endpoint_attributes(endpoint_arn)
    # print(str(r))

    # 删除endpoint信息
    # aws_sns_service.sns_delete_endpoint(endpoint_arn)

    # # 创建endpointA
    # r = aws_sns_service.sns_create_platform_endpoint(
    #     appid='ios.alivemovie.com',
    #     token='24f71f424cc64a7beabfe8f34b221c3bb7daccf696ec359dc739e84f1da7c180',
    # )
    # print(str(r))

    # aws_sns_service.sns_publish(endpoint_arn, '')


